<cfcomponent displayname="users gateway" hint="">

	<cffunction name="init" access="public" returntype="usersGateway" output="false" displayname="users Constructor" hint="I initialize a users.">
		<cfargument name="datasourceSettings" type="any" required="true" />
		
		<cfset variables.dsn = arguments.datasourceSettings.getConfigSetting("dsn") />
		<cfset variables.dbtype = arguments.datasourceSettings.getConfigSetting("dbtype") />

		<cfreturn this />
	</cffunction>

	<cffunction name="getAll" access="public" returntype="query" output="false" displayname="" hint="">

		<cfset var usersSelect = 0 />
		<cfquery name="usersSelect" datasource="#variables.dsn#">
			SELECT * 
			FROM	users	
		</cfquery>

		<cfreturn usersSelect/>
	</cffunction>
	
	<cffunction name="getPasswordSalt" returntype="string" access="public" output="false" hint="I retrieve the password salt for a username.">
		<cfargument name="username" type="string" required="true"/>
		<cfset var usersSelect = 0 />
		<cfset var passwordSalt = createUUID() />
		
		<cfquery name="usersSelect" datasource="#variables.dsn#">
			SELECT	passwordSalt
			FROM	users
			WHERE	username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#">
		</cfquery>
		<cfif usersSelect.recordcount gt 0>
			<cfset passwordSalt = usersSelect.passwordSalt/>
		</cfif>
		
		<cfreturn passwordSalt />
	</cffunction>
	
	<cffunction name="Authenticate" returntype="query" access="public" output="false" hint="I Authenticate a user.">
		<cfargument name="username" type="string" required="true"/>
		<cfargument name="password" type="string" required="true"/>
		<cfset var usersSelect = 0 />
		
		<cfquery name="usersSelect" datasource="#variables.dsn#">
			SELECT	userid
			FROM	users
			WHERE	username = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.username#">
					and password = <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.password#">
		</cfquery>
		
		<cfreturn usersSelect />
	</cffunction>	

</cfcomponent>
